package com.example.em.controller;

import com.example.em.model.Attendance;
import com.example.em.service.AttendanceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;
import java.time.LocalTime;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/attendance")
public class AttendanceController {
    @Autowired
    private AttendanceService attendanceService;
    
    @GetMapping
    public ResponseEntity<?> getAllAttendance() {
        try {
            List<Map<String, Object>> attendance = attendanceService.getAllAttendanceWithEmployeeInfo();
            return ResponseEntity.ok(attendance);
        } catch (Exception e) {
            return ResponseEntity.internalServerError().body("获取考勤记录失败: " + e.getMessage());
        }
    }
    
    @GetMapping("/{id}")
    public ResponseEntity<?> getAttendance(@PathVariable Long id) {
        try {
            Attendance attendance = attendanceService.getAttendance(id);
            if (attendance == null) {
                return ResponseEntity.notFound().build();
            }
            return ResponseEntity.ok(attendance);
        } catch (Exception e) {
            return ResponseEntity.internalServerError().body("获取考勤记录失败: " + e.getMessage());
        }
    }
    
    @GetMapping("/employee/{employeeId}")
    public ResponseEntity<?> getAttendanceByEmployee(@PathVariable Long employeeId) {
        try {
            List<Map<String, Object>> attendance = attendanceService.getAttendanceByEmployee(employeeId);
            return ResponseEntity.ok(attendance);
        } catch (Exception e) {
            return ResponseEntity.internalServerError().body("获取教师考勤记录失败: " + e.getMessage());
        }
    }
    
    @GetMapping("/date/{date}")
    public ResponseEntity<?> getAttendanceByDate(@PathVariable String date) {
        try {
            List<Map<String, Object>> attendance = attendanceService.getAttendanceByDate(date);
            return ResponseEntity.ok(attendance);
        } catch (Exception e) {
            return ResponseEntity.internalServerError().body("获取指定日期考勤记录失败: " + e.getMessage());
        }
    }
    
    @PostMapping
    public ResponseEntity<?> addAttendance(@RequestBody Attendance attendance) {
        try {
            attendanceService.addAttendance(attendance);
            return ResponseEntity.ok().build();
        } catch (Exception e) {
            return ResponseEntity.internalServerError().body("添加考勤记录失败: " + e.getMessage());
        }
    }
    
    @PutMapping("/{id}")
    public ResponseEntity<?> updateAttendance(@PathVariable Long id, @RequestBody Attendance attendance) {
        try {
            attendance.setId(id);
            attendanceService.updateAttendance(attendance);
            return ResponseEntity.ok().build();
        } catch (Exception e) {
            return ResponseEntity.internalServerError().body("更新考勤记录失败: " + e.getMessage());
        }
    }
    
    @DeleteMapping("/{id}")
    public ResponseEntity<?> deleteAttendance(@PathVariable Long id) {
        try {
            attendanceService.deleteAttendance(id);
            return ResponseEntity.ok().build();
        } catch (Exception e) {
            return ResponseEntity.internalServerError().body("删除考勤记录失败: " + e.getMessage());
        }
    }
    
    @GetMapping("/departments")
    public ResponseEntity<?> getAllDepartments() {
        try {
            List<String> departments = attendanceService.getAllDepartments();
            return ResponseEntity.ok(departments);
        } catch (Exception e) {
            return ResponseEntity.internalServerError().body("获取学院列表失败: " + e.getMessage());
        }
    }
    
    @PostMapping("/add")
    public ResponseEntity<?> addAttendanceRecord(@RequestBody Map<String, Object> request) {
        try {
            Attendance attendance = new Attendance();
            attendance.setEmployeeId(Long.parseLong(request.get("employeeId").toString()));
            attendance.setDate(LocalDate.parse(request.get("date").toString()));
            attendance.setCheckIn(LocalTime.parse(request.get("checkIn").toString()));
            attendance.setCheckOut(LocalTime.parse(request.get("checkOut").toString()));
            
            // 根据签到时间自动判断状态
            LocalTime standardTime = LocalTime.of(8, 30);
            LocalTime standardEndTime = LocalTime.of(17, 0);
            
            if (attendance.getCheckIn().isAfter(standardTime)) {
                attendance.setStatus("迟到");
            } else if (attendance.getCheckOut().isBefore(standardEndTime)) {
                attendance.setStatus("早退");
            } else {
                attendance.setStatus("正常");
            }
            
            attendanceService.addAttendance(attendance);
            return ResponseEntity.ok().build();
        } catch (Exception e) {
            return ResponseEntity.internalServerError().body("添加考勤记录失败: " + e.getMessage());
        }
    }
    
    @DeleteMapping("/today")
    public ResponseEntity<?> deleteTodayAttendance() {
        try {
            attendanceService.deleteTodayAttendance();
            return ResponseEntity.ok().build();
        } catch (Exception e) {
            return ResponseEntity.internalServerError().body("删除今日考勤记录失败: " + e.getMessage());
        }
    }
} 